package com.ourteam.product.api;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractCRUDServiceController;
import com.ourteam.frame.web.OperationResult;
import com.ourteam.product.ui.dao.ProductUIPageParamDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIPageTemplateParamDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUIPageBean;
import com.ourteam.product.ui.domain.ProductUIPageParamBean;
import com.ourteam.product.ui.domain.ProductUIPageTemplateParamBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

@Controller
@RequestMapping("/api/PRODUCT/UI/ProductUIPageParam")
public class ProductUIPageParamAPI extends
		AbstractCRUDServiceController<ProductUIPageParamDAOQueryBean, ProductUIPageParamSearchModel, ProductUIPageParamBean> {

	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	@Override
	protected ProductUIPageParamDAOQueryBean newQueryBean() {
		return new ProductUIPageParamDAOQueryBean();
	}

	@Override
	protected int queryCount(ProductUIPageParamDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUIPageParamCount(queryBean);
	}

	@Override
	protected ProductUIPageParamBean[] queryData(ProductUIPageParamDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUIPageParams(queryBean);
	}

	@Override
	protected void deleteData(long[] ids) throws Exception {
		PRODUCT_UI_SERVICE.deleteProductUIPageParams(ids);
	}

	@Override
	protected ProductUIPageParamBean getDataById(long id) throws Exception {
		return PRODUCT_UI_SERVICE.getProductUIPageParamById(id);
	}

	@Override
	protected void saveData(ProductUIPageParamBean dataBean) throws Exception {
		if (dataBean.getId() == 0) {
			PRODUCT_UI_SERVICE.addNewProductUIPageParam(dataBean);
		} else {
			PRODUCT_UI_SERVICE.modifyProductUIPageParam(dataBean);
		}
	}

	@RequestMapping("/synchPageParamters")
	@ResponseBody
	public OperationResult synchPageParamters(@RequestParam long pageId) throws Exception {

		ProductUIPageBean pageBean = PRODUCT_UI_SERVICE.getProductUIPageById(pageId);

		ProductUIPageTemplateParamDAOQueryBean queryBean = new ProductUIPageTemplateParamDAOQueryBean();

		queryBean.createCriteria().andPageTemplateIdEqualTo(pageBean.getPageTemplateId());

		ProductUIPageTemplateParamBean[] templateParamBeans = PRODUCT_UI_SERVICE
				.queryProductUIPageTemplateParams(queryBean);

		// Map<Long, ProductUIPageTemplateParamBean> map = new HashMap<>();

		ProductUIPageParamDAOQueryBean paramQueryBean = new ProductUIPageParamDAOQueryBean();

		paramQueryBean.createCriteria().andPageIdEqualTo(pageBean.getId());

		ProductUIPageParamBean[] paramBeans = PRODUCT_UI_SERVICE.queryProductUIPageParams(paramQueryBean);

		Map<Long, ProductUIPageParamBean> map = new HashMap<>();

		for (int i = 0; i < paramBeans.length; i++) {
			map.put(paramBeans[i].getPageParamId(), paramBeans[i]);
		}

		for (int i = 0; i < templateParamBeans.length; i++) {

			if (map.containsKey(templateParamBeans[i].getId()) == false) {
				ProductUIPageParamBean paramBean = new ProductUIPageParamBean();

				paramBean.setPageId(pageBean.getId());
				paramBean.setPageParamId(templateParamBeans[i].getId());

				PRODUCT_UI_SERVICE.addNewProductUIPageParam(paramBean);
			}
		}

		return OperationResult.SUCCESS_RESULT;

	}

}
